package com.opennews.openplatform.controller;

import com.opennews.openplatform.dto.journey.CreateJourneyDto;
import com.opennews.openplatform.dto.journey.UpdateJourneyDto;
import com.opennews.openplatform.myspringbootcore.dto.ApiResponse;
import com.opennews.openplatform.myspringbootcore.dto.IdentityDto;
import com.opennews.openplatform.myspringbootmvccore.helper.HttpResponseHelper;
import com.opennews.openplatform.myspringbootmvccore.security.RequestIdentityExtractor;
import com.opennews.openplatform.service.JourneyService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RequiredArgsConstructor
@RestController
@RequestMapping("/journey")
public class JourneyController {
    private final JourneyService journeyService;
    private final RequestIdentityExtractor requestIdentityExtractor;
    private final HttpResponseHelper httpResponseHelper;

    @GetMapping("/query-list-by-account-group")
    public ResponseEntity<ApiResponse<List<Map<String, Object>>>> queryListByAccountGroup(@RequestParam int pageSize, @RequestParam int pageIndex) {
        String accountGroupId = requestIdentityExtractor.getClaims().accountGroupId;
        List<Map<String, Object>> result = journeyService.queryListByAccountGroup(accountGroupId, pageSize, pageIndex);

        return httpResponseHelper.getSuccessResponse(result);
    }

    @PostMapping("/add")
    public ResponseEntity<ApiResponse<Object>> add(@Valid @RequestBody CreateJourneyDto data) {
        String accountGroupId = requestIdentityExtractor.getClaims().accountGroupId;
        journeyService.add(accountGroupId, data);

        return httpResponseHelper.getSuccessResponse();
    }

    @PostMapping("/update")
    public ResponseEntity<ApiResponse<Object>> update(@Valid @RequestBody UpdateJourneyDto data) {
        String accountGroupId = requestIdentityExtractor.getClaims().accountGroupId;
        journeyService.update(accountGroupId, data);

        return httpResponseHelper.getSuccessResponse();
    }

    @PostMapping("/delete")
    public ResponseEntity<ApiResponse<Object>> delete(@Valid @RequestBody IdentityDto data) {
        String id = data.getId();
        String accountGroupId = requestIdentityExtractor.getClaims().accountGroupId;
        journeyService.delete(accountGroupId, id);

        return httpResponseHelper.getSuccessResponse();
    }
}
